package com.hckj.jianzhijia.networks;

import android.os.AsyncTask;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.hckj.jianzhijia.model.City;
import com.hckj.jianzhijia.model.Region;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵奇隆 on 2015-11-28.
 */
public class GetRegionNetWork extends AsyncTask<Void,Void,ArrayList<Region>> {

    private int cityId = -1;
    private NetWorkInterface netWorkInterface;

    public GetRegionNetWork(int cityId,NetWorkInterface netWorkInterface) {
        this.cityId = cityId;
        this.netWorkInterface = netWorkInterface;
    }

    @Override
    protected ArrayList<Region> doInBackground(Void... params) {

        AVQuery avObjectAVQuery = AVQuery.getQuery(Region.TABLE_NAME);
        avObjectAVQuery.whereEqualTo("belongTo",cityId);
        ArrayList<Region> regions = new ArrayList<Region>();
        try {
            List list = avObjectAVQuery.find();
            for(int i=0,j=list.size();i<j;i++){
                regions.add(new Region((AVObject)(list.get(i))));
            }
            return regions;
        } catch (AVException e) {
            return null;
        }

    }

    @Override
    protected void onPostExecute(ArrayList<Region> regions) {
        netWorkInterface.afterGetRegions(regions);
        super.onPostExecute(regions);
    }

    public interface NetWorkInterface{
        public void afterGetRegions(ArrayList<Region> regions );
    }
}
